<?php 
/**
 * 分类管理控制器
 * @author 董满 <970473068@qq.com>
 */
class CategoryController extends Controller{
	private $model;
	public function __init(){
		parent::__init();
		$this->model=K('Category');		
	}
    /**
     * 分类列表
     */
    public function index(){
    	if(IS_POST){
			$sorts=Q("post.");
			foreach ($sorts as $cid => $csort) {
				$this->model->where("cid={$cid}")->update(array('csort'=>$csort));
			}
			$this->success('排序成功');
    	}
    	//获得数据
    	$data = $this->model->all();
		//树状结构
		$data = Data::tree($data,'cname');
		$this->assign('data',$data);
        $this->display(); 
    }
	/**
	 * 添加
	 */
	 public function add(){
	 	if(IS_POST){
	 		if(!$this->model->addCate()) $this->error($this->model->error);
			$this->success('添加成功',U('index'));
	 	}
	     $this->display();
	 }
	 /**
	  * 修改
	  */
	  public function edit(){
	  	if(IS_POST){
	  		if(!$this->model->editCate()) $this->error($this->model->error);
			$this->success('修改成功',U('index'));
	  	}
	  	//获取cid
	  	$cid=Q('get.cid',0,'intval');
		p($cid);
		//获得旧数据
		$oldData=$this->model->where("cid={$cid}")->find();
		p($oldData);
		$this->assign('oldData',$oldData);
	      $this->display();
	  }
	  /**
	   * 删除
	   */
	   public function del(){
	   	//获得cid
	   	$cid=Q('get.cid',0,'intval');
		$son=$this->model->where("pid={$cid}")->find();
		if($son) $this->error('请先删除子集');
		//删除分类
		$this->model->where("cid={$cid}")->delete();
		$this->success('删除成功');
	   }
	   /**
	    * 添加子分类
	    */
	    public function addSon(){
	    	if(IS_POST){
	    		if(!$this->model->addCate()) $this->error($this->model->error);
				$this->success('添加成功',U('index'));
	    	}
	    	$cid=Q('get.cid',0,'intval');
			$this->assign('cid',$cid);
			//获得所有数据
			$allData=$this->model->all();
			$this->assign('allData',$allData);
			//处理父级分类
			$fatherCate=$this->model->where("cid={$cid}")->find();
			$this->assign('fatherCate',$fatherCate);
			//删除自己本身
			$cids[]=$cid;
			$data=$this->model->where("cid NOT IN(".implode(',',$cids).")")->all();
			$data = Data::tree($data,'cname');
			$this->assign('CateData',$data);
			
	        $this->display();
	    }

 }
 
 
 
 
 
 
 
 
 
 
 

 ?>